<?php
/**
 * 商品
 * @copyright  Copyright (c) 2014-2030 muxiangdao-cn Inc.(http://www.muxiangdao.cn)
 * @license    http://www.muxiangdao.cn
 * @link       http://www.muxiangdao.cn
 * @author	   muxiangdao-cn Team
 */
namespace Toadmin\Controller;
use Think\Page;
class FirmwareController extends GlobalController {
	public function _initialize() 
	{
        parent::_initialize();
	}	
	//分类
	public function firmware()
	{
        $map = array();
        $name = trim($_GET['name']);

        if($name)$map['name'] = array('like','%'.$name.'%');

        if(!empty($_GET['class'])){
            $map['class'] = array('like','%'.$_GET['class'].'%');
        }
        if(!empty($_GET['ver'])){
            $map['ver'] = array('like','%'.$_GET['ver'].'%');
        }

        $totalRows = M('firmware')->where($map)->count();
        $page = new Page($totalRows,10);
        $list = M('firmware')->where($map)->limit($page->firstRow.','.$page->listRows)->order('ctime desc')->select();
        $this->assign('list',$list);
        $this->assign('search',$_GET);
        $this->assign('page_show',$page->show());
		$this->display();		
	}
    public function firmware_add()
	{
		if(IS_POST)
		{
			$map=array();
			$map['name']      = str_rp(trim($_POST['name']));
            $map['ver']      = str_rp(trim($_POST['ver']));
            $map['class'] = str_rp($_POST['class']);
            $map['download'] = str_rp($_POST['download']);
            $map['describe'] = str_rp($_POST['describe']);
            $map['ctime'] = time();
            $map['utime'] = time();
            $return = M('firmware')->add($map);
			if($return)
			{
				/*if(!empty($_FILES['download']['name']))
	            {
	            	$upname = 'download'.time();
	            	$param = array('savePath'=>'firmware/','subName'=>'','files'=>$_FILES['download'],'saveName'=>$upname,'saveExt'=>'');
	            	$up_return = upload_one($param);
	            	if($up_return == 'error')
	            	{
	            		$this->error('固件上传失败');
	            		exit;
	            	}else{
	            		$data['download'] = $up_return;
                        M('firmware')->where('id='.$return)->save($data);
	            	}
	            }*/
				$this->success('添加成功！', U('firmware'));
			}else{
				$this->error('添加失败！');		
			}
		}else{
			$this->display();
		}
    }	


	
	//编辑分类
	public function firmware_edit()
	{
		if(IS_POST && $_POST['id'])
		{
			$data = array();

			$data['name'] = str_rp($_POST['name']);
			$data['ver'] = str_rp(trim($_POST['ver']));
            $data['class'] = str_rp($_POST['class']);
			$data['describe'] = str_rp($_POST['describe']);
            $data['download'] = str_rp($_POST['download']);
            $data['utime'] = time();
            //图片上传
            /*if(!empty($_FILES['download']['name']))
            {
                $upname = 'download'.time();
            	$param = array('savePath'=>'firmware/','subName'=>'','files'=>$_FILES['download'],'saveName'=>$upname,'saveExt'=>'');
            	$up_return = upload_one($param);
            	if($up_return == 'error')
            	{
            		$this->error('固件上传失败');
            		exit;
            	}else{
            		$data['download'] = $up_return;
            	}
            }*/

            $rlt = M("firmware")->where('id='.$_POST['id'])->save($data);
            if ($rlt) {
            	$this->success('操作成功！', U('firmware'));
            }else {
            	$this->error('操作失败！');
            }
            exit;
		}else{
			$id = intval($_GET['id']);
			$rs = M("firmware")->where('id='.$id)->find();
			$this->assign('rs',$rs);	
			$this->display();	
		}
	}	
	//删除分类信息
	public function firmware_del()
	{
		$id = $_GET['id'];
		if($id)
		{
			$map = array();
			$map['id'] = array('in',$id);
			$delnum = M("firmware")->where($map)->delete();
			if($delnum)
			{
				$this->success('操作成功！', U('firmware'));
			}else{
				$this->error('操作失败！');
			}
		}
	}
}